package com.huaxin.hxcloud.find;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.common.Entity.FindSearchBean;
import com.huaxin.common.message.view.CircleImageView;
import com.huaxin.common.utils.StringUtils;
import com.huaxin.hxcloud.R;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hebing on 2017/5/11.
 */

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<FindSearchResultBean>                            datas;
    private Context                                               context;
    private SearchResultFragment.OnSearchResultItemClickListenner itemClickListenner;

    public SearchAdapter(Context context, List<FindSearchResultBean> datas, SearchResultFragment.OnSearchResultItemClickListenner itemClickListenner) {
        this.context = context;
        this.datas = datas;
        this.itemClickListenner = itemClickListenner;

    }

    public List<FindSearchResultBean> getDatas() {
        if (datas == null)
            return null;
        return datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.find_search_title, parent, false);
            viewHolder = new SearchTitle(view);
        }
        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.find_search_people_item, parent, false);
            viewHolder = new SearchPeople(view);
        }
        if (viewType == 2) {
            View view = LayoutInflater.from(context).inflate(R.layout.find_search_zixun_item, parent, false);
            viewHolder = new SearchZiXun(view);
        }
        if (viewType == 3) {
            View view = LayoutInflater.from(context).inflate(R.layout.question_re_item, parent, false);
            viewHolder = new SearchProblem(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final FindSearchResultBean resultBean = datas.get(position);
        //标题
        if (getItemViewType(position) == 0) {
            SearchTitle viewHolder = (SearchTitle) holder;
            viewHolder.titleTv.setText(resultBean.getTitle());
        }
        //人物
        if (getItemViewType(position) == 1) {
            SearchPeople viewHolder = (SearchPeople) holder;
            FindSearchBean.UserArrayBean userArrayBean = resultBean.getUser();
            viewHolder.niceName.setText(userArrayBean.getNickname());
            Glide.with(context).load(userArrayBean.getImg())
                    .apply(new RequestOptions().placeholder(R.drawable.default_image)).into(viewHolder.imageIcon);

            //Glide.with(context).load(userArrayBean.getImg()).placeholder(R.drawable.default_image).into(viewHolder.imageIcon);
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListenner.onItemClick(1, resultBean, position);
                }
            });
        }
        //资讯
        if (getItemViewType(position) == 2) {
            SearchZiXun viewHolder = (SearchZiXun) holder;
            FindSearchBean.InformationArrayBean informationArrayBean = resultBean.getInformation();
            viewHolder.textTile.setText(informationArrayBean.getTitle());
            viewHolder.textTime.setText(com.huaxin.common.utils.StringUtils.friendlyTime(new Date(informationArrayBean.getDate())));
            Glide.with(context).load(informationArrayBean.getImg()).apply(new RequestOptions().placeholder(R.drawable.default_image)).into(viewHolder.imageIcon);
            //Glide.with(context).load(informationArrayBean.getImg()).placeholder(R.drawable.default_image).into(viewHolder.imageIcon);
            viewHolder.textContent.setText(informationArrayBean.getDescription());
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListenner.onItemClick(2, resultBean, position);
                }
            });
        }
        //问题
        if (getItemViewType(position) == 3) {
            SearchProblem viewHolder = (SearchProblem) holder;
            FindSearchBean.QuestionArrayBean questionArrayBean = resultBean.getQuestion();

            viewHolder.textContent.setText(questionArrayBean.getContent());
            Glide.with(context).load(questionArrayBean.getPublisher().getImg()).apply(new RequestOptions().placeholder(R.drawable.default_image)).into(viewHolder.circleImageView);

            //Glide.with(context).load(questionArrayBean.getPublisher().getImg()).placeholder(R.drawable.default_image).into(viewHolder.circleImageView);
            viewHolder.textTime.setText("" + StringUtils.friendlyTime(new Date(questionArrayBean.getPublishDate())) + "\t\t" + questionArrayBean.getCommentNum() + "人回复");
            List<String> imgUrls = questionArrayBean.getImgUrls();
            if (imgUrls != null) {
                if (imgUrls.size() != 0) {
                    viewHolder.gridView.setVisibility(View.VISIBLE);
                    viewHolder.gridView.setAdapter(new GridViewAdapter(imgUrls, context));
                } else {
                    viewHolder.gridView.setVisibility(View.GONE);
                }
            }
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListenner.onItemClick(3, resultBean, position);
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
        int type = datas.get(position).getType();
        switch (type) {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 2;
            case 3:
                return 3;
        }
        return 110;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    //
    class SearchTitle extends RecyclerView.ViewHolder {

        @BindView(R.id.find_search_titles)
        TextView titleTv;

        public View mView;

        public SearchTitle(View itemView) {
            super(itemView);
            this.mView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    //
    class SearchPeople extends RecyclerView.ViewHolder {
        @BindView(R.id.search_people_item_icon)
        CircleImageView imageIcon;
        @BindView(R.id.search_peaple_item_name)
        TextView        niceName;

        public View mView;

        public SearchPeople(View itemView) {
            super(itemView);
            this.mView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    //
    class SearchZiXun extends RecyclerView.ViewHolder {
        @BindView(R.id.search_zixun_item_icon)
        ImageView imageIcon;
        @BindView(R.id.search_zixun_item_title)
        TextView  textTile;
        @BindView(R.id.search_zixun_item_time)
        TextView  textTime;
        @BindView(R.id.search_zixun_item_content)
        TextView  textContent;

        public View mView;

        public SearchZiXun(View itemView) {
            super(itemView);
            this.mView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    //
    class SearchProblem extends RecyclerView.ViewHolder {
        @BindView(R.id.question_re_contentv)
        TextView        textContent;
        @BindView(R.id.question_re_gv)
        FindGridView    gridView;
        @BindView(R.id.question_re_cimg)
        CircleImageView circleImageView;
        @BindView(R.id.question_re_timetv)
        TextView        textTime;

        public View mView;

        public SearchProblem(View itemView) {
            super(itemView);
            this.mView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
